layui.config({
    base: '/plugins/module/'
});
var uploadUrl;
layui.use(['table', 'layer', 'form', 'cstmodules', 'upload'], function () {
    var mymod = layui.cstmodules;
    var $ = layui.jquery;
    var table = layui.table;
    var layer = layui.layer;
    var form = layui.form;
    var upload = layui.upload;
    var mainIndex = null;
    var numberConfig = {
        param: {
            tableColumn: [
                [
                    {field: 'id', sort: true, title: '标识编码', align: 'center'}
                    , {field: 'name', title: '名字', align: 'center'}
                    , {field: 'development', title: '部门', align: 'center'}
                    , {field: 'major', title: '专业', align: 'center'}
                    , {field: 'classes', title: '班级', align: 'center'}
                    , {title: '操作', toolbar: '#id_numberConfig', align: 'center'}
                ]
            ],
            limit: 500,
            limits: [500, 800, 900, 1000]
        },

        //删除
        deleteNumber: function (data) {
            layer.confirm('你确定要删除【' + data.name + '】参与抽奖吗', {icon: 3, title: '提示'}, function (index) {
                $.post("/reward/deleteNumberConfig", {id: data.id}, function (res) {
                    if (res.code === 1) {
                        table.reload('tab_numberConfig');
                    }
                    layer.msg(res.info);
                })
                layer.close(index);
            })
        },
        //清空数据
        cleanNumberConfig: function () {
            layer.confirm('你确定要清空参与抽奖吗', {icon: 3, title: '提示'}, function (index) {
                $.post("/reward/cleanNumberConfig", function (res) {
                    if (res.code === 0) {
                        layer.msg(res.info);
                        table.reload('tab_numberConfig');
                    } else {
                        layer.msg('清空失败！！！');
                    }
                });
                layer.close(index);
            })
        },
        modifyNumber: function (data) {
            mainIndex = layer.open({
                type: 1,
                content: $("#numberConfigDiv"),
                area: ['900px', '500px'],
                title: '修改成员信息',
                skin: "layui-layer-lan",
                success: function () {
                    $("#dataFrm")[0].reset();
                    //装载新的数据
                    form.val("dataFrm", data);
                    $("#number_id").attr("disabled", "disabled");
                    url = "/reward/updateNumberConfig";
                }
            });
        },
        addNumberConfig: function (data) {
            mainIndex = layer.open({
                type: 1,
                content: $("#numberConfigDiv"),
                area: ['750px', '450px'],
                title: '添加成员',
                skin: "layui-layer-lan",
                success: function () {
                    $("#dataFrm")[0].reset();
                    $("#number_id").removeAttr("disabled");
                    url = "/reward/insertNumberConfig";
                }
            });
        },
        batchAddNumberConfig: function () {
            mainIndex = layer.open({
                type: 1,
                content: $("#addOrUpdateDiv"),
                area: ['450px', '175px'],
                title: '批量添加学生',
                skin: "layui-layer-lan",
                success: function () {
                    $("#dataFrm1")[0].reset();
                }
            });
        }
    };

    mymod.initPageTableMethod(table, "tab_numberConfig", "/reward/numberConifg", numberConfig.param.tableColumn, "#addLevel", null, numberConfig.param.limit, numberConfig.param.limits, 95);

    //监听工具条
    table.on('tool(f-numberConfig)', function (obj) {
        var data = obj.data;
        switch (obj.event) {
            case 'delete':
                numberConfig.deleteNumber(data);
                break;
            case 'modify':
                numberConfig.modifyNumber(data);
                break;
        }
        ;
    });

    //监听提交
    form.on('submit(doSubmit)', function (data) {
        $.post(url, data.field, function (res) {
            if (res.code === 1) {
                table.reload('tab_numberConfig');
            }
            layer.close(mainIndex);
            layer.msg(res.info);
        })
        return false;
    });

    form.on('submit(doSubmitUpload)', function (data) {
        var f_content = data.field.file;
        var fileext = f_content.substring(f_content.lastIndexOf("."), f_content.length)
        fileext = fileext.toLowerCase()
        console.log(fileext === '.xlsx')
        if (fileext === '.xlsx' || fileext === '.xls') {
            layer.confirm('你确定要导入这个文件吗?', {btn: ['确定', '取消']}, function (index) {

                    var formData = new FormData();
                    var excelFile = $("#uploadExcel")[0].files[0];
                    formData.append("excelFile", excelFile);
                    $.ajax({
                        url: "/reward/uploadNumberConifg",
                        type: 'POST',
                        dataType: 'json',
                        data: formData,
                        cache: false,
                        processData: false,
                        contentType: false,
                        success: function (responseStr) {
                            if (responseStr.code === 1) {
                                layer.alert(responseStr.info, 5, 1);
                                table.reload('tab_numberConfig');
                                layer.close(mainIndex);
                            } else {
                                layer.alert(responseStr.info, 5, 1);
                                layer.close(mainIndex);
                            }
                        },
                        error: function (responseStr) {
                            layer.alert("失败");
                            layer.close(mainIndex);
                        }
                    });
                    return true;
                },
                function (index) {
                    layer.msg('已取消！', {icon: 1});
                    return false;
                });
        } else {
            layer.alert("对不起，导入数据格式必须是xls格式文件哦，请您调整格式后重新上传，谢谢 ！");
            return false;
        }
    });


    //监听头部工具栏
    table.on('toolbar(f-numberConfig)', function (obj) {
        switch (obj.event) {
            case 'add':
                numberConfig.addNumberConfig();
                break;
            case 'batchAdd':
                numberConfig.batchAddNumberConfig();
                break;
            case 'cleanData':
                numberConfig.cleanNumberConfig();
        }
        ;
    });
});
